package com.ganee.agora.view

import android.os.Bundle
import android.text.*
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.CompoundButton
import android.widget.TextView.OnEditorActionListener
import androidx.lifecycle.ViewModelProviders
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.ganee.agora.R
import com.ganee.agora.model.OriginalLoginRequestBody
import com.ganee.agora.viewmodel.LoginViewModel
import com.ganee.baselib.arouter.Constance
import com.ganee.baselib.mvvmlibrary.base.BaseActivity
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_original_login.*
import kotlinx.android.synthetic.main.toolbar_with_title.*
import okhttp3.MediaType
import okhttp3.RequestBody


/**
 * 原生登录页面
 */
@Route(path = Constance.ACTIVITY_URL_LOGIN_ORIGINAL)
class OriginalLoginActivity : BaseActivity<LoginViewModel>(),View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_original_login)
        ARouter.getInstance().inject(this)
        initView()
    }

    private fun initView() {
        toolbarCancel.setOnClickListener(this)
        toolbarTitle.text = "Login"
        login_btn_login.setOnClickListener(this)
        val watch = WatchChange()
//        login_et_username.addTextChangedListener(watch)
//        login_et_password.addTextChangedListener(watch)

        login_et_password.setOnEditorActionListener(OnEditorActionListener { v, actionId, event ->
            if (!TextUtils.isEmpty(v.text.toString())) {
                if (actionId == EditorInfo.IME_ACTION_DONE || event != null && event.keyCode == KeyEvent.KEYCODE_ENTER) {
                    initRequestLogin()
                }
            }
            false
        })

        loginIsShowPw.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { compoundButton, b ->
            if (loginIsShowPw.isChecked) { // 设置EditText文本为可见的
                login_et_password.transformationMethod = HideReturnsTransformationMethod.getInstance()
            } else { // 设置EditText文本为隐藏的
                login_et_password.transformationMethod = PasswordTransformationMethod.getInstance()
            }
            login_et_password.postInvalidate()
            // 切换后将EditText光标置于末尾
            val charSequence: CharSequence = login_et_password.getText()
            if (charSequence is Spannable) {
                Selection.setSelection(charSequence, charSequence.length)
            }
        })
    }

    override fun getViewModel(): LoginViewModel {
        return ViewModelProviders.of(this@OriginalLoginActivity).get(LoginViewModel::class.java)
    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.toolbarCancel -> {
                finish()
            }
            R.id.login_btn_login -> {
                initRequestLogin()
            }
        }
    }


    private fun initRequestLogin() {
        val account = login_et_username.text.toString().trim()
        val password = login_et_password.text.toString().trim()
        val loginBody = OriginalLoginRequestBody(account,password,"03")
        val body = Gson().toJson(loginBody)
        val requestBody = RequestBody.create(MediaType.parse("application/json"), body)
//        viewModel.login(requestBody)
        viewModel.register(account, password)
    }

    inner class WatchChange : TextWatcher{
        override fun afterTextChanged(e: Editable?) {
            if(login_et_username.length() > 0 && login_et_password.length() > 0){
                login_btn_login.setBackgroundResource(R.drawable.login_bg_btn)
                login_btn_login.isEnabled = true;
            }else {
                login_btn_login.setBackgroundResource(R.drawable.bg_btn_rectangle_login_grey)
                login_btn_login.isEnabled = false;
            }
        }

        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        }

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        }

    }
}
